package com.ruoyi.baselibrary.infobasecategory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.baselibrary.infobasecategory.domain.InfoBaseCategory;
import com.ruoyi.baselibrary.infobasecategory.service.IInfoBaseCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基础库类别树信息Controller
 *
 * @author ruoyi
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/InfoBaseCategory/InfoBaseCategory")
public class InfoBaseCategoryController extends BaseController {
    @Autowired
    private IInfoBaseCategoryService infoBaseCategoryService;

    /**
     * 查询基础库类别树信息列表
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoBaseCategory infoBaseCategory) {
        List<InfoBaseCategory> list = infoBaseCategoryService.selectInfoBaseCategoryList(infoBaseCategory);
        return getDataTable(list);
    }

    /**
     * 导出基础库类别树信息列表
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:export')")
    @Log(title = "基础库类别树信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBaseCategory infoBaseCategory) {
        List<InfoBaseCategory> list = infoBaseCategoryService.selectInfoBaseCategoryList(infoBaseCategory);
        ExcelUtil<InfoBaseCategory> util = new ExcelUtil<InfoBaseCategory>(InfoBaseCategory.class);
        util.exportExcel(response, list, "基础库类别树信息数据");
    }

    /**
     * 获取基础库类别树信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBaseCategoryService.selectInfoBaseCategoryById(id));
    }

    /**
     * 新增基础库类别树信息
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:add')")
    @Log(title = "基础库类别树信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBaseCategory infoBaseCategory) {
        return toAjax(infoBaseCategoryService.insertInfoBaseCategory(infoBaseCategory));
    }

    /**
     * 修改基础库类别树信息
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:edit')")
    @Log(title = "基础库类别树信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBaseCategory infoBaseCategory) {
        try{
            infoBaseCategoryService.updateInfoBaseCategory(infoBaseCategory);
            return AjaxResult.success("修改成功");
        }catch (Exception e){
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除基础库类别树信息
     */
    @PreAuthorize("@ss.hasPermi('InfoBaseCategory:InfoBaseCategory:remove')")
    @Log(title = "基础库类别树信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBaseCategoryService.deleteInfoBaseCategoryByIds(ids));
    }

    /**
     * 获取某一种类的数据
     *
     * @param category
     * @return
     */
    @RequestMapping(value = "/getCategories", method = RequestMethod.GET)
    public AjaxResult getCategories(@RequestParam("category") String category) {
        InfoBaseCategory infoBaseCategory = new InfoBaseCategory();
        infoBaseCategory.setCategory(category);
        List<InfoBaseCategory> infoBaseCategories = infoBaseCategoryService.selectInfoBaseCategoryList(infoBaseCategory);
        return AjaxResult.success(infoBaseCategories);
    }

}
